<template>
  <div style="width: 100%;height: 100%">
    <div id="imgContainer"><img src="../assets/fudan.jpg" alt="Fudan University"></div>
    <div id="loginBox">
      <div style="color: grey;font-size: 30px; text-align: center;padding: 30px">欢迎登陆</div>
      <el-form :model="form" size="normal">
        <el-form-item>
          <el-icon style="color: grey">
            <avatar/>
          </el-icon>
          <el-input v-model="form.username" placeholder="用户名" style="width: 90%"/>
        </el-form-item>
        <el-form-item>
          <el-icon style="color: grey">
            <key/>
          </el-icon>
          <el-input v-model="form.password" placeholder="密码" style="width: 90%" show-password/>
        </el-form-item>
        <el-form-item>
          <el-radio v-model="form.type" :label=0>学生</el-radio>
          <el-radio v-model="form.type" :label=1>教师</el-radio>
          <el-radio v-model="form.type" :label=3>管理员</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%" type="primary" @click="login">登录</el-button>
        </el-form-item>
<!--/*        <el-button style="color: #cccccc" size="small" @click="$router.push('/resetPassword')">*/-->
<!--/*          新用户初次登陆请修改密码*/-->
<!--/*        </el-button>*/-->
      </el-form>
    </div>
  </div>
</template>

<script>
import avatar from "@element-plus/icons-vue/dist/es/avatar.mjs";
import key from "@element-plus/icons-vue/dist/es/key.mjs";
import request from "@/utils/request";

export default {
  name: "LoginView",
  components: {
    avatar,
    key
  },

  data() {
    return {
      avatar: {},
      form: {
        type:0
      },
    }
  },
  created() {
    document.title='登录'
  },
  methods: {
    login() {
      request.post("http://124.70.179.188:8082/login", this.form).then(res => {
        // console.log(!res.data.flag)
        if (res.code === 0) {
          this.$message({
            type: "success",
            message: "登录成功"
          })
          localStorage.setItem("token", res.data.token)
          localStorage.setItem("uid", res.data.uid)
          // localStorage.setItem("username", res.data.username)
          localStorage.setItem("type", this.form.type)
          localStorage.setItem("userid",this.form.username)
          console.log(JSON.stringify(localStorage))
          if (!res.data.flag) {
            this.$router.push("/resetPassword")
            return
          }
          if (this.form.type === 0) {
            this.$router.push("/welStu")
          }
          if (this.form.type === 1) {
            this.$router.push("/welTea")
          }
          if (this.form.type === 3) {
            this.$router.push("/welAdmin")
          }
        } else {
          this.$message({
            type: "error",
            message: res.message
          })
        }
      })
    }
  }
}
</script>

<style scoped>
#imgContainer {
  float: left;
  width: 50%;
  height: fit-content;
  overflow: clip;
}

img {
  width: auto;
  height: 607px;
}

#loginBox {
  width: auto;
  padding-top: 100px;
  margin-left: 55%;
  margin-right: 5%;
}
</style>